package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.woke.method.Md5jarss;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

public class CheckpaypswActivity extends Activity implements Callback, OnClickListener {

    private TextView mT1, mT2, mT3, mT4, mT5, mT6, mT7, mT8, mT9, mT11;
    private Handler mHandler;
    private String aaaa = "";
    private RelativeLayout mT12;//�h���I
    private TextView mTtext1, mTtext3, mTtext2, mTtext4, mTtext5, mTtext6;
    private String psw1 = "";
    private String psw2 = "";
    private String psw3 = "";
    private String psw4 = "";
    private String psw5 = "";
    private String psw6 = "";
    private MyApp application;
    private String carno;
    private View mBar;
    private String paymoneys;
    private boolean isright = false;//�ж�֧�������Ƿ���ȷ
    private Intent intent;
    private int phonewid;
    private FrameLayout mFrLayout;
    private int phoneheigh;
    private String cxtong;
    public static final int RESULT_CODE_PAY_PASSWORD_IS_RIGHT = 600;
    public String payType = "";//类型, 充值chongzhi还是支付
    public static final String KEY_PAY_TYPE = "payType";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_checkpaypsw);

//		
        intent = getIntent();
        paymoneys = intent.getStringExtra("paymoney");
        cxtong = "" + intent.getStringExtra("cxtong");
        payType = intent.getStringExtra("payType");
        intview();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        intent.putExtra("paypsw", isright);
        setResult(RESULT_CODE_PAY_PASSWORD_IS_RIGHT, intent);
        finish();
    }

    private void intview() {
        application = (MyApp) getApplication();
        phonewid = application.screenWidth;
        phoneheigh = application.screenHigth;
        mHandler = new Handler(CheckpaypswActivity.this);
        findViewById(R.id.avchhenckc_image_back).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mFrLayout = (FrameLayout) findViewById(R.id.avcheck_paypsw_frame);
        LayoutParams params = mFrLayout.getLayoutParams();
        params.width = phonewid;
        params.height = phoneheigh / 3 * 2;
        mFrLayout.setLayoutParams(params);
        mBar = findViewById(R.id.avjiebangc_progress);

        mT1 = (TextView) findViewById(R.id.checkpsw_edit_1);
        mT2 = (TextView) findViewById(R.id.checkpsw_edit_2);
        mT3 = (TextView) findViewById(R.id.checkpsw_edit_3);
        mT4 = (TextView) findViewById(R.id.checkpsw_edit_4);
        mT5 = (TextView) findViewById(R.id.checkpsw_edit_5);
        mT6 = (TextView) findViewById(R.id.checkpsw_edit_6);
        mT7 = (TextView) findViewById(R.id.checkpsw_edit_7);
        mT8 = (TextView) findViewById(R.id.checkpsw_edit_8);
        mT9 = (TextView) findViewById(R.id.checkpsw_edit_9);
        mT11 = (TextView) findViewById(R.id.checkpsw_edit_11);
        mT12 = (RelativeLayout) findViewById(R.id.checkpsw_edit_12);

        mTtext1 = (TextView) findViewById(R.id.check_psw_1);
        mTtext2 = (TextView) findViewById(R.id.check_psw_2);
        mTtext3 = (TextView) findViewById(R.id.check_psw_3);
        mTtext4 = (TextView) findViewById(R.id.check_psw_4);
        mTtext5 = (TextView) findViewById(R.id.check_psw_5);
        mTtext6 = (TextView) findViewById(R.id.check_psw_6);

        mT1.setOnClickListener(this);
        mT2.setOnClickListener(this);
        mT3.setOnClickListener(this);
        mT4.setOnClickListener(this);
        mT5.setOnClickListener(this);
        mT6.setOnClickListener(this);
        mT7.setOnClickListener(this);
        mT8.setOnClickListener(this);
        mT9.setOnClickListener(this);
        mT11.setOnClickListener(this);
        mT12.setOnClickListener(this);

    }

    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == 0x101) {
            int arg1 = msg.arg1;
            aaaa = aaaa + arg1;
            aaaa = aaaa.trim();
//			for (int i = 0; i < aaaa.length(); i++) {
            if (aaaa.length() == 1) {
                psw1 = "" + arg1;
                mTtext1.setText(psw1);
            } else if (aaaa.length() == 2) {
                psw2 = "" + arg1;
                mTtext2.setText(psw2);
            } else if (aaaa.length() == 3) {
                psw3 = "" + arg1;
                mTtext3.setText(psw3);
            } else if (aaaa.length() == 4) {
                psw4 = "" + arg1;
                mTtext4.setText(psw4);
            } else if (aaaa.length() == 5) {
                psw5 = "" + arg1;
                mTtext5.setText(psw5);
            } else if (aaaa.length() == 6) {
                psw6 = "" + arg1;
                mTtext6.setText(psw6);
            }
            String trim1 = mTtext1.getText().toString().trim();
            String trim2 = mTtext2.getText().toString().trim();
            String trim3 = mTtext3.getText().toString().trim();
            String trim4 = mTtext4.getText().toString().trim();
            String trim5 = mTtext5.getText().toString().trim();
            String trim6 = mTtext6.getText().toString().trim();
            if (!trim1.equals("") && !trim2.equals("") && !trim3.equals("") && !trim4.equals("")
                    && !trim5.equals("") && !trim6.equals("")) {
                String get32md5 = Md5jarss.get32MD5("sp_");
                String get32md52 = Md5jarss.get32MD5(aaaa);
                String sp1 = get32md5.substring(0, 12);
                String sp11 = get32md5.substring(get32md5.length() - 4, get32md5.length());

                String pswold = sp1 + get32md52 + sp11;
//
                if (application.getDatas_load() == null) {
                    return false;
                }
                if (!pswold.equals(application.getDatas_load().getPay_pwd())) {
                    Toast.makeText(CheckpaypswActivity.this, "支付密码不正确", Toast.LENGTH_SHORT)
                            .show();
//					finish();
                } else {
                    if ("chongzhi".equals(payType)) {
                        //充值只验证密码
                        isright = true;
                        intent.putExtra("paypsw", isright);
                        setResult(RESULT_CODE_PAY_PASSWORD_IS_RIGHT, intent);
                        finish();
                    } else {
                        isright = true;
                        String getuser_money = application.getDatas_load().getUser_money();

                        if (cxtong.equals("cxtong")) {
                            Log.e("cxtong", cxtong);
                            Intent intent = new Intent();
                            intent.setAction("com.woke.cxtongactivity");
                            intent.putExtra("phoneno", "phoneno");
                            sendBroadcast(intent);
                            finish();
                        } else if (cxtong.equals("liuliang")) {
                            Intent intent = new Intent();
                            intent.setAction("com.woke.cxtonglactivity");
                            intent.putExtra("phoneno", "phoneno");
                            sendBroadcast(intent);
                            finish();
                        } else if (cxtong.equals("wulianwang")) {
                            Intent intent = new Intent();
                            intent.setAction("com.woke.cxtongwuactivity");
                            intent.putExtra("phoneno", "phoneno");
                            sendBroadcast(intent);
                            finish();
                        } else {
                            intent.putExtra("paypsw", isright);
                            setResult(RESULT_CODE_PAY_PASSWORD_IS_RIGHT, intent);
                            finish();
                        }
                    }

                }
                aaaa = trim1 + trim2 + trim3 + trim4 + trim5 + trim6;
            }

        } else {
            Log.e("後退", "後退");
            if (!aaaa.equals("")) {
//				aaaa = aaaa.substring(0, aaaa.length() - 1);
                aaaa = aaaa.trim();
                if (aaaa.length() == 1) {
                    Log.e("1", "1");
                    aaaa = "";
                    mTtext1.setText(aaaa);
                } else if (aaaa.length() == 2) {
                    Log.e("2", "2");
                    aaaa = aaaa.substring(0, 1);
                    mTtext2.setText("");
                } else if (aaaa.length() == 3) {
                    aaaa = aaaa.substring(0, 2);
                    Log.e("3", "3");
                    mTtext3.setText("");
                } else if (aaaa.length() == 4) {
                    Log.e("4", "4");
                    aaaa = aaaa.substring(0, 3);
                    mTtext4.setText("");
                } else if (aaaa.length() == 5) {
                    Log.e("5", "5");
                    aaaa = aaaa.substring(0, 4);
                    mTtext5.setText("");
                } else if (aaaa.length() == 6) {
                    Log.e("6", "6");
                    aaaa = aaaa.substring(0, 5);
                    mTtext6.setText("");
                }
            } else {

            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        Message msg = mHandler.obtainMessage();
        switch (v.getId()) {
            case R.id.checkpsw_edit_1:
//			String t1 = mT1.getText().toString();
//			aaaa = aaaa + t1;
                msg.what = 0x101;
                msg.arg1 = 1;
                mHandler.sendMessage(msg);
                break;

            case R.id.checkpsw_edit_2:
//			String t2 = mT2.getText().toString();
//			aaaa = aaaa + t2;
                msg.what = 0x101;
                msg.arg1 = 2;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_3:
//			String t3 = mT3.getText().toString();
//			aaaa = aaaa + t3;
                msg.what = 0x101;
                msg.arg1 = 3;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_4:
//			String t4 = mT4.getText().toString();
//			aaaa = aaaa + t4;
                msg.what = 0x101;
                msg.arg1 = 4;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_5:
//			String t5 = mT5.getText().toString();
//			aaaa = aaaa + t5;
                msg.what = 0x101;
                msg.arg1 = 5;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_6:
//			String t6 = mT6.getText().toString();
//			aaaa = aaaa + t6;
                msg.what = 0x101;
                msg.arg1 = 6;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_7:
//			String t7 = mT7.getText().toString();
//			aaaa = aaaa + t7;
                msg.what = 0x101;
                msg.arg1 = 7;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_8:
//			String t8 = mT8.getText().toString();
//			aaaa = aaaa + t8;
                msg.what = 0x101;
                msg.arg1 = 8;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_9:
//			String t9 = mT9.getText().toString();
//			aaaa = aaaa + t9;
                msg.what = 0x101;
                msg.arg1 = 9;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_11:
//			String t11 = mT11.getText().toString();
                msg.what = 0x101;
                msg.arg1 = 0;
                mHandler.sendMessage(msg);
                break;
            case R.id.checkpsw_edit_12:
                // String t12 = mT1.getText().toString();
                msg.what = 0x102;
                mHandler.sendMessage(msg);
                break;

            default:
                break;
        }
    }

}
